﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class UserCenter_user_OrderList : System.Web.UI.Page
{
    BLL.Business.OrderBLL orderBLL = new BLL.Business.OrderBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
        if (!object.Equals(Session["UserName"], null))
        { }
        else
        {
            Response.Write("<script>window.open('user_Login.aspx')</script>");
        }
    }

    public void BindData()
    {
        try
        {
            if (!object.Equals(Session["UserName"], null))
            {
                DataSet ds = orderBLL.SelectByUserName(Session["UserName"].ToString());
                if ((ds == null) ||
                    (ds.Tables.Count == 0) ||
                    (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                {
                    this.lable1.Visible = true;
                }
                else
                {
                    ds.Tables[0].Columns.Add("orderStates");           // 添加新列（表示订单状态）
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        if (dr["Ord_Statusint"].ToString() == "0")
                        {
                            dr["orderStates"] = "交易失败";
                        }
                        else if (dr["Ord_Statusint"].ToString() == "1")
                        {
                            dr["orderStates"] = "订单成功";
                        }
                        else
                        {
                            dr["orderStates"] = "交易成功";
                        }
                    }
                    orderList.DataSource = ds;
                    orderList.DataBind();
                }
            }
            else
            { //Response.Redirect("user_login.aspx");
                Response.Write("<script>window.open('user_Login.aspx')</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }

    protected void orderList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "deleteProduct")
        {
            try
            {
                int orderId = Int32.Parse(e.CommandArgument.ToString());
                if (orderBLL.DeleteOrder(orderId))
                {
                    BindData();
                }
                else
                {
                    Response.Write("<script>alter('出现错误，请检查！')</script>");
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
            }
        }
    }
}